package com.cmccgz.udacs_landlord_webapp.main.user.login;


import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.cmccgz.baselibrary.utils.RegularUtil;
import com.cmccgz.baselibrary.utils.StringUtils;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.base.GlobalContext;
import com.cmccgz.udacs_landlord_webapp.bean.ResultBean;

import java.util.Timer;
import java.util.TimerTask;

import rx.Subscriber;

/**
 * @author INX
 * 忘记密码presenter
 */
public class ForgetPresenter implements LoginContract.ForgetPresenter {
    private LoginContract.ForgetView forgetView;
    private Timer timer;
    private int waitTime = 60;
    private boolean isWaiting=false;
    public ForgetPresenter(LoginContract.ForgetView forgetView) {
        this.forgetView = forgetView;
        forgetView.setPresenter(this);
    }

    @Override
    public void forgetPwd(String userName, String pwd, String rePwd, String code) {
        Context context = GlobalContext.getInstance();
        if (!RegularUtil.isMobileNumber(userName)) {
            forgetView.forgetError(context.getString(R.string.tips_phone_error));
            return;
        }
        if (!RegularUtil.isPassword(pwd)) {
            forgetView.forgetError(context.getString(R.string.tips_pwd_error));
            return;
        }
        if (!pwd.equals(rePwd)) {
            forgetView.forgetError(context.getString(R.string.tips_re_pwd_dissame));
            return;
        }
        if (StringUtils.isEmpty(code)) {
            forgetView.forgetError(context.getString(R.string.tips_code_empty));
            return;
        }
        //验证验证码的正确性
        ApiService.resetPD(userName,pwd,code)
                .subscribe(new Subscriber<ResultBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        forgetView.forgetError("重置密码失败！");
                    }

                    @Override
                    public void onNext(ResultBean resultBean) {
                        if ("0".equals(resultBean.rsmsg.rscode)) {
                            forgetView.forgetSuccess();
                        }else{
                            forgetView.forgetError(resultBean.rsmsg.rsdesc);
                        }
                    }
                });
    }

    @Override
    public void getCode(String mobile,final Handler handler) {
        Context context = GlobalContext.getInstance();
        if (!RegularUtil.isMobileNumber(mobile)) {
            forgetView.getCodeError(context.getString(R.string.tips_phone_error));
            return;
        }

        startWaiting(handler);

        //getcode
        ApiService.getSms(mobile,"1").subscribe(new Subscriber<ResultBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                stopWaiting();
                forgetView.getCodeError("获取验证码失败！");
            }

            @Override
            public void onNext(ResultBean resultBean) {
                if ("0".equals(resultBean.rsmsg.rscode)) {
                    forgetView.getCodeSuccess();
                }else{
                    stopWaiting();
                    forgetView.getCodeError(resultBean.rsmsg.rsdesc);
                }
            }
        });
    }

    public void startWaiting(final Handler handler) {
        if (timer != null) {
            timer.cancel();
        }
        waitTime = 60;
        forgetView.waitCode(waitTime );
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                waitTime--;
                if (handler != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Log.e("INX", "waitTime: isWaiting="+isWaiting+",waitTime="+waitTime );

                            if (!isWaiting) {
                                return;
                            }
                            forgetView.waitCode(waitTime );
                        }
                    });
                }
                if (waitTime <= 0) {
                    timer.cancel();
                }
            }
        }, 1000, 1000);
        isWaiting=true;
    }

    public void stopWaiting() {
        if (timer!=null) {
            timer.cancel();
        }
        timer=null;
        forgetView.resetCode();
        isWaiting=false;
    }

    @Override
    public void onDestroy() {
        stopWaiting();
    }
    @Override
    public void start() {

    }
}
